<template>
  <div class="app-container">
    <el-dialog v-dialogDrag :visible.sync="dialogVisible" :title="title" width="950px" @close="resetForm('ruleForm')">
      <button-group ref="buttonGroup" @submitForm="submitForm('ruleForm')" @cancel="dialogVisible = false" />
      <div class="formTopFont">物料登记单</div>
      <el-row>
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="80px">
          <el-tabs type="card" tab-position="top">
            <el-tab-pane label="基本信息">
              <el-row>
                <el-col :span="12">
                  <el-form-item label="物料名称" prop="materialName">
                    <el-input v-model="ruleForm.materialName" maxlength="20" show-word-limit />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="物料编码" prop="materialCode">
                    <el-input v-model="ruleForm.materialCode" maxlength="20" show-word-limit />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="物料分类" prop="classifyId">
                    <tree-select ref="treeSelect" style="width: 100%" :props="{
                    value: 'id',
                    label: 'classifyName',
                    children: 'children'
                  }" :options="treeData" :disabled="false" :accordion="true" :value="ruleForm.classifyId" search-key="classifyName" @getNode="getValue($event)" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="品牌型号" prop="model">
                    <el-input v-model="ruleForm.model" maxlength="20" show-word-limit />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="辅助代码" prop="auxiliaryCode">
                    <el-input v-model="ruleForm.auxiliaryCode" maxlength="20" show-word-limit />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="单位" prop="unit">
                    <el-input v-model="ruleForm.unit" maxlength="20" show-word-limit />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="材料" prop="texture">
                    <el-input v-model="ruleForm.texture" show-word-limit />
                  </el-form-item>
                </el-col>
                <el-col :span="12" style="padding:8px 0 0 0;">
                  <el-row>
                    <el-col :span="12" style="text-align:center;">
                      <el-checkbox v-model="ruleForm.isRaw">原材料</el-checkbox>
                    </el-col>
                    <el-col :span="12" style="text-align:center;">
                      <el-checkbox v-model="ruleForm.isCheck">检验</el-checkbox>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
            </el-tab-pane>
            <el-tab-pane label="其他信息">
              <el-row>
                <el-col :span="8">
                  <el-form-item label="最小包装" prop="minPackaging">
                    <el-input v-model="ruleForm.minPackaging" maxlength="20" show-word-limit />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="助记代码" prop="mnemonicCode">
                    <el-input v-model="ruleForm.mnemonicCode" maxlength="20" show-word-limit />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="数量" prop="num">
                    <el-input v-model="ruleForm.num" maxlength="20" show-word-limit />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="8">
                  <el-form-item label="价格" prop="price">
                    <el-input v-model="ruleForm.price" maxlength="20" show-word-limit />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="采购周期" prop="procurementCycle">
                    <el-input v-model="ruleForm.procurementCycle" maxlength="20" show-word-limit />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="规格描述" prop="specifications">
                    <el-input v-model="ruleForm.specifications" maxlength="20" show-word-limit />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="8">
                  <el-form-item label="宽" prop="width">
                    <el-input v-model="ruleForm.width" maxlength="20" show-word-limit />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="长" prop="length">
                    <el-input v-model="ruleForm.length" maxlength="20" show-word-limit />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="高" prop="height">
                    <el-input v-model="ruleForm.height" maxlength="20" show-word-limit />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-col :span="24">
                <el-form-item label="备注" prop="remark">
                  <el-input v-model="ruleForm.remark" type="textarea" rows="3" maxlength="500" show-word-limit />
                </el-form-item>
              </el-col>
            </el-tab-pane>
          </el-tabs>
        </el-form>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import TreeSelect from '@/components/TreeSelect'
export default {
  components: { TreeSelect },
  data() {
    const checkName = async (rule, value, callback) => {
      const res = await this.$axios.get(
        `/material/info/verifyNameUnique?materialName=${value}&id=${this.ruleForm.id}`
      )
      if (res?.data.code === 200) {
        return res.data.data
          ? callback()
          : new Error(callback('物料名称重复！'))
      } else {
        return new Error(callback(res.data.msg))
      }
    }
    const checkCode = async (rule, value, callback) => {
      const res = await this.$axios.get(
        `/material/info/verifyCodeUnique?materialCode=${value}&id=${this.ruleForm.id}`
      )
      if (res?.data.code === 200) {
        return res.data.data
          ? callback()
          : new Error(callback('物料编码重复！'))
      } else {
        return new Error(callback(res.data.msg))
      }
    }
    return {
      title: '',
      ruleForm: {
        materialName: '',
        materialCode: '',
        classifyId: '',
        model: '',
        texture: '',
        parentName: '',
        id: '',
        auxiliaryCode: '',
        minPackaging: '',
        mnemonicCode: '',
        num: '',
        price: '',
        procurementCycle: '',
        specifications: '',
        width: '',
        length: '',
        height: '',
        isRaw: true,
        isCheck: true
      },
      rules: {
        materialName: [
          { required: true, message: '物料名称不能为空！', trigger: 'blur' },
          { validator: checkName, trigger: 'blur' }
        ],
        materialCode: [
          { required: true, message: '物料编码不能为空！', trigger: 'blur' },
          { validator: checkCode, trigger: 'blur' }
        ],
        classifyId: [
          { required: true, message: '请选择物料分类！', trigger: 'change' }
        ]
      },
      dialogVisible: false,
      treeData: []
    }
  },
  watch: {},
  mounted() {},
  methods: {
    /**
     * @description: 新增编辑
     * @param {*} row
     * @return {*}
     */
    showDialog(row, data) {
      this.dialogVisible = true
      this.treeData = [...data]
      if (row && row.hasOwnProperty('id')) {
        this.title = '编辑'
        row.isRaw = row.isRaw === 1 ? true : false
        row.isCheck = row.isCheck === 1 ? true : false
        this.ruleForm = Object.assign({}, row)
      } else {
        this.title = '新增'
      }
    },
    /**
     * @description: 获取选中的上级部门
     * @param {*} value
     * @return {*}
     */
    getValue(value) {
      if (value) {
        this.ruleForm.classifyId = value.id
        this.ruleForm.classifyName = value.classifyName
      } else {
        this.ruleForm.classifyId = ''
        this.ruleForm.classifyName = ''
      }
    },
    /**
     * @description: 表单提交
     * @param {*} formName
     * @return {*}
     */
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          this.ruleForm.isRaw = this.ruleForm.isRaw === true ? 1 : 0
          this.ruleForm.isCheck = this.ruleForm.isCheck === true ? 1 : 0
          const res = await this.$axios.post(
            '/material/info/save',
            this.ruleForm
          )
          if (res?.data.code === 200) {
            this.$notify.success('提交成功！')
            this.dialogVisible = false
            this.$emit('getTableInfo')
          } else {
            this.$notify.error(res.data.msg)
          }
        }
      })
    },
    /**
     * @description: 重置表单
     * @param {*} formName
     * @return {*}
     */
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.ruleForm = {
        materialName: '',
        materialCode: '',
        classifyId: '',
        model: '',
        texture: '',
        parentName: '',
        id: '',
        auxiliaryCode: '',
        minPackaging: '',
        mnemonicCode: '',
        num: '',
        price: '',
        procurementCycle: '',
        specifications: '',
        width: '',
        length: '',
        height: '',
        isRaw: true,
        isCheck: true
      }
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
